import { createRouter, createWebHistory, RouterView } from 'vue-router';
import { useRouterAfterEach, useRouterBeforeEach, useRouterOnError } from '@gis/lib/router/index.js';
import env from '@gis/lib/env.js';

const router = createRouter({
  history: createWebHistory(env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'root',
      meta: { title: 'root', checkToken: true, permission: true },
      component: RouterView,
    },
    {
      path: '/login',
      name: 'login',
      meta: { title: '登录', checkToken: false, permission: true },
      component: () => import('@gis/lib/views/login/index.vue'),
    },
    {
      path: '/recover',
      name: 'recover',
      meta: { title: '重置密码', checkToken: false, permission: true },
      component: () => import('@gis/lib/views/login/Recover.vue'),
    },
    {
      path: '/thirdparty',
      name: 'thirdparty',
      meta: { title: '三方页面', checkToken: false, permission: true },
      component: () => import('@gis/lib/views/third-party/index.vue'),
    },
    {
      path: '/404',
      name: '404',
      meta: { title: '404', checkToken: false, permission: true },
      component: () => import('@gis/lib/views/404.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
    },
  ],
});

router.onError(async (error, to, from) => {
  await useRouterOnError(error, to, from);
});

router.beforeEach(async (to, from, next) => {
  await useRouterBeforeEach(to, from, next);
});

router.afterEach(async (to, from) => {
  await useRouterAfterEach(to, from);
});

export default router;
